import VGE from '../../VGE';

export class ViewSetting {
	constructor(viewer, iCesium) {
		this.viewer = viewer;
		this.iCesium = iCesium;
		this.layerQueue = viewer.scene.layers.layerQueue;
		this.skyBoxSources = Object.assign({}, viewer.scene.skyBox.sources);
	}

	/**
	 * 罗盘显示
	 * @param visible
	 */
	setCampassVisible(visible) {
		const dom = document.querySelector('.sm-compass');
		dom.style.display = visible ? 'block' : 'none';
	}

	setHighlightColor(color) {
		const iCesium = this.iCesium;
		const viewer = this.viewer;

		const primaryColor = iCesium.Color.fromCssColorString(color || VGE.config.primaryColor);
		for (const layer of viewer.scene.layers.layerQueue) {
			layer.selectColorType = iCesium.SelectColorType.ALWAYS_SHOW_SILHOUETTE;
			layer.selectedColor = primaryColor;
			layer.selectedLineColor = primaryColor;
		}
	}

	setMultiChoose(flag) {
		this.layerQueue.forEach((item) => {
			item.multiChoose = flag;
		});
	}

	setLayerSelectEnabled(enable) {
		this.layerQueue.forEach((item) => {
			item.selectEnabled = enable;
			!enable && item.releaseSelection();
		});
	}

	/**
	 * 设置构件（图元）透明度
	 * @param {Array} ids 构件（图元）id数组
	 * @param {Number} value 透明度值，[0,1]
	 * @returns void
	 */
	setComponentsTransparent(ids, value) {
		const iCesium = this.iCesium;
		for (const layer of this.layerQueue) {
			if (!value) return;
			layer.setObjsColor(ids, iCesium.Color.WHITE.withAlpha(Number(value)));
		}
	}

	/**
	 * 设置构件（图元）颜色
	 * @param {Array} ids 构件（图元）id数组
	 * @param {String} value 颜色值  "rgb(255,0,0)" "rgba(255,0,0,.2)" "rgb(100%,0%,0%)" "hsl(120,50%,50%)" "#rrggbb" "#rgb"
	 * @returns void
	 */
	setComponentsColor(ids, value) {
		const iCesium = this.iCesium;
		for (const layer of this.layerQueue) {
			if (!value) return;
			layer.setObjsColor(ids, iCesium.Color.fromCssColorString(value));
		}
	}

	/**
	 * 清除构件（图元）被设置的颜色/透明度
	 * @param {Array} ids 构件（图元）id数组
	 */
	removeComponentsColor(ids) {
		for (const layer of this.layerQueue) {
			layer.removeObjsColor(ids);
		}
	}

	/**
	 * 清除所有构件（图元）被设置的颜色/透明度
	 */
	removeAllComponentsColor() {
		for (const layer of this.layerQueue) {
			layer.removeAllObjsColor();
		}
	}

	/**
	 * 获取构件（图元）颜色值
	 * @param {Number} id 构件（图元）id
	 * @returns
	 */
	getComponentColor(id) {
		const data = [];
		for (const layer of this.layerQueue) {
			const color = layer.getObjsColor(id);
			data.push(color);
		}
		return data;
	}

	/**
	 * 获取构件（图元）可见性
	 * @param {Number} id 构件（图元）id
	 * @returns {Boolean} 可见性值
	 */
	getComponentVisible(id) {
		const data = [];
		for (const layer of this.layerQueue) {
			const color = layer.getObjsVisible(id);
			data.push(color);
		}
		return data;
	}

	/**
	 * 设置构件（图元）可见性
	 * @param {Array} ids 构件（图元）id数组
	 * @param {Boolean} value 可见性值，true可见，false隐藏
	 * @returns void
	 */
	setComponentsVisible(ids, value) {
		for (const layer of this.layerQueue) {
			layer.setOnlyObjsVisible(ids, value);
		}
	}

	/**
	 * 设置构件（图元）隔离
	 * @param {Array} ids 构件（图元）id数组
	 */
	setComponentsIsolate(ids) {
		for (const layer of this.layerQueue) {
			layer.setObjsVisible(ids, true);
		}
	}

	/**
	 * 渲染样式
	 */
	setSmoothShadeStyle() {
		for (const layer of this.layerQueue) {
			layer.style3D.lineWidth = 0.1;
			layer.style3D.fillStyle = 0;
		}
	}

	/**
	 * 线框样式
	 */
	setWireframeStyle() {
		for (const layer of this.layerQueue) {
			layer.style3D.lineWidth = 1;
			layer.style3D.fillStyle = 1;
		}
	}

	/**
	 * 设置天空盒
	 * @param skyBoxSources
	 */
	setSkyBox(skyBoxSources = {}) {
		this.viewer.scene.backgroundColor = new this.iCesium.Color(0, 0, 0, 1);
		const skyBoxOptions = {
			show: true,
			sources: Object.assign({}, this.skyBoxSources, skyBoxSources)
		};
		this.viewer.scene.skyBox = new this.iCesium.SkyBox(skyBoxOptions);
	}

	/**
	 * 设置背景色
	 * @param color {string}
	 */
	setBackgroundColor(color = '#dae7f0') {
		this.viewer.scene.skyBox.show = false;
		if (color instanceof this.iCesium.Color) {
			this.viewer.scene.backgroundColor = color;
		} else {
			this.viewer.scene.backgroundColor = this.iCesium.Color.fromCssColorString(color);
		}
	}
}
